package com.amir.common.utils

import android.text.Editable
import android.text.InputFilter
import android.text.Spanned
import android.text.TextWatcher
import android.widget.EditText

class MoneyEdiTextUtil {

    companion object{


        /**
         * 设置edittext只能输入小数点后两位
         */
        fun afterDotTwo(editText: EditText) {


            editText.setFilters(arrayOf<InputFilter>(object : InputFilter {
                override fun filter(
                    source: CharSequence,
                    start: Int,
                    end: Int,
                    dest: Spanned,
                    dstart: Int,
                    dend: Int
                ): CharSequence? {
                    if (source == "." && dest.toString().length === 0) {
                        return "0."
                    }
                    if (dest.toString().contains(".")) {
                        val index = dest.toString().indexOf(".")
                        val mlength = dest.toString().substring(index).length
                        if (mlength == 3) {
                            return ""
                        }
                    }
                    return null
                }
            }))



            editText.addTextChangedListener(object : TextWatcher {
                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                    var s = s
                    // 限制最多能输入9位整数
                    if (s.toString().contains(".")) {
                        if (s.toString().indexOf(".") > 9) {
                            s = s.toString().subSequence(
                                0,
                                9
                            ).toString() + s.toString().substring(s.toString().indexOf("."))
                            editText.setText(s)
                            editText.setSelection(9)
                        }
                    } else {
                        if (s.toString().length > 9) {
                            s = s.toString().subSequence(0, 9)
                            editText.setText(s)
                            editText.setSelection(9)
                        }
                    }
                    // 判断小数点后只能输入两位
                    if (s.toString().contains(".")) {
                        if (s.length - 1 - s.toString().indexOf(".") > 2) {
                            s = s.toString().subSequence(
                                0,
                                s.toString().indexOf(".") + 3
                            )
                            editText.setText(s)
                            editText.setSelection(s.length)
                        }
                    }
                    //如果第一个数字为0，第二个不为点，就不允许输入
                    if (s.toString().startsWith("0") && s.toString().trim { it <= ' ' }.length > 1) {
                        if (s.toString().substring(1, 2) != ".") {
                            editText.setText(s.subSequence(0, 1))
                            editText.setSelection(1)
                            return
                        }
                    }
                }

                override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

                }

                override fun afterTextChanged(s: Editable) {
                    if (editText.getText().toString().trim() != null && !editText.getText().toString().trim().equals(
                            ""
                        )
                    ) {
                        if (editText.getText().toString().trim().substring(0, 1).equals(".")) {
                            editText.setText("0" + editText.getText().toString().trim())
                            editText.setSelection(1)
                        }
                    }
                }
            })
        }
    }
}